//
//  QRCodeCardViewController.swift
//  DSWeibo
//
//  Created by J on 16/4/16.
//  Copyright © 2016年 J. All rights reserved.
//

import UIKit
import SnapKit
class QRCodeCardViewController: UIViewController {

    private lazy var iconView :UIImageView = UIImageView()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        //1.设置标题
        navigationItem.title = "我的名片"
        //2.添加图片容器
        
        view.addSubview(iconView)
        
        //3.布局图片容器
        
        iconView.snp_makeConstraints { (make) in
            make.size.equalTo(CGSizeMake(300, 300))
            make.center.equalTo(view)
        }
        
        //4.生成二维码
        
        let qrcodeImage = createQRCodeImage()
        
        
        //5.将生成的二维码添加到图片容器上
        iconView.image = qrcodeImage
    
    
    }
    
    func createQRCodeImage() -> UIImage?{
        //创建滤镜
        
        let filter = CIFilter(name: "CIQRCodeGenerator")
        
        //还原滤镜默认属性
        filter?.setDefaults()
        
        
        //设置要生成的二维码的数据
        
        filter?.setValue("极客江南".dataUsingEncoding(NSUTF8StringEncoding), forKey: "inputMessage")
        let ciImage = filter?.outputImage
        let bgImage = createNonInterpolatedUIImageFormCIImage(ciImage!, size: 300)
            
        
        //从滤镜中取出生成好的图片
        
        let icon = UIImage(named: "tableview_loading")
        //创建一个头像
        let newNewImage = createImage(bgImage,icon:icon!)
        //合成图片
        
        return newNewImage
        //返回生成好的二维码图片
        

        
        
    }
    
   private  func createImage(bgImage:UIImage,icon:UIImage)->UIImage{
    
    
    UIGraphicsBeginImageContext(bgImage.size)
    
    bgImage.drawInRect(CGRect(origin: CGPointZero, size: bgImage.size))
    
    
    //绘制头像
    let width : CGFloat = 50
    let height : CGFloat = width
    let x = 0.5 * (bgImage.size.width - width)
    let y = 0.5 * (bgImage.size.height - height)

    icon.drawInRect(CGRectMake(x, y, width, height))
    
    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return newImage
        
        
        
    }
    
    /**
     根据CIImage生成指定大小的高清UIImage
     
     :param: image 指定CIImage
     :param: size    指定大小
     :returns: 生成好的图片
     */
    private func createNonInterpolatedUIImageFormCIImage(image: CIImage, size: CGFloat) -> UIImage {
        
        let extent: CGRect = CGRectIntegral(image.extent)
        let scale: CGFloat = min(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent))
        
        // 1.创建bitmap;
        let width = CGRectGetWidth(extent) * scale
        let height = CGRectGetHeight(extent) * scale
        let cs: CGColorSpaceRef = CGColorSpaceCreateDeviceGray()!
        let bitmapRef = CGBitmapContextCreate(nil, Int(width), Int(height), 8, 0, cs, 0)!
        
        let context = CIContext(options: nil)
        let bitmapImage: CGImageRef = context.createCGImage(image, fromRect: extent)
        
        CGContextSetInterpolationQuality(bitmapRef,  CGInterpolationQuality.None)
        CGContextScaleCTM(bitmapRef, scale, scale);
        CGContextDrawImage(bitmapRef, extent, bitmapImage);
        
        // 2.保存bitmap到图片
        let scaledImage: CGImageRef = CGBitmapContextCreateImage(bitmapRef)!
        
        return UIImage(CGImage: scaledImage)
    }
}
